<?php
require_once('lib/category.php');
$cat = cat_get_by_id($_GET['id']);
if ($cat == NULL) {
	?>
	<p class="error">Cette catégorie horaire n'existe pas</p>
	<?php
	return;
}

$name = $cat['name'];
$age_max = $cat['age_max'];
$age_min = $cat['age_min'];
$description = $cat['description'];
$max_places = $cat['max_places'];

if (isset($_POST['submit-category'])) {
	$name = clean_str($_POST['name']);
	$age_min = intval($_POST['age_min']);
	$age_max = intval($_POST['age_max']);
	$description = clean_str($_POST['description']);
	$max_places = intval($_POST['max_places']);
	
	$errors = array();
	if ($name == '') {
		$errors[] = 'Vous devez indiquer le nom du créneau horaire';
	}
	if ($age_min == 0) {
		$errors[] = 'Vous devez indiquer l\'age minimum';
	}
	if ($age_max == 0) {
		$errors[] = 'Vous devez indiquer l\'age maximum';
	}
	
	if (count($errors) == 0 &&
		cat_update($_GET['id'], $name, $age_min, $age_max, $description, $max_places)) {
		
		echo '<p class="success">Le créneau horaire a été mis à jour</p>';
	}
	else {
		echo '<div class="error">Merci de corriger les erreurs suivantes :';
		print_array($errors);
		echo '</div>';
	}
}

?>
<h3>Catégorie de créneaux horaires : <?php echo $cat['name'] ?></h3>

<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post" class="form-content">
	<table>
		<tr>
			<td>Nom du créneau : </td>
			<td><input type="text" name="name" value="<?php echo $name ?>" /></td>
		</tr>
		<tr>
			<td>Age minimum : </td>
			<td><input type="text" name="age_min" value="<?php echo $age_min ?>" class="js-numeric" /></td>
		</tr>
		<tr>
			<td>Age maximum : </td>
			<td><input type="text" name="age_max" value="<?php echo $age_max ?>" class="js-numeric" /></td>
		</tr>
		<tr>
			<td>Nombre de places max (falcultatif) :</td>
			<td><input type="text" name="max_places" value="<?php echo $max_places ? $max_places : '' ?>" class="js-numeric" /></td>
		</tr>
		<tr>
			<td>Description (facultatif) : </td>
			<td><textarea name="description" cols="40" rows="5"><?php echo $description ?></textarea></td>
		</tr>
		
	</table>
	<p class="center">
		<input type="submit" value="Mettre à jour" name="submit-category" />
	</p>
</form>

<p class="back">
	<a href="?p=club&amp;r=hours">Revenir à la liste des créneaux horaires</a>
</p>

